package com.example.administrator.familfinalcialmanagement.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.familfinalcialmanagement.Constants;
import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.StroeBean;
import com.example.administrator.familfinalcialmanagement.databinding.ItemStoreBinding;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.listener.BaseRecyclerViewHolder;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.List;

/**
 * Created by 86178 on 2019/11/26.
 * author mogen
 * 用心，成就一切。
 */

public class StoreAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<StroeBean.GoodsListBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public StoreAdapter(Context context, List<StroeBean.GoodsListBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemStoreBinding functionBinding = DataBindingUtil.inflate( LayoutInflater.from(mcontext), R.layout.item_store, parent, false);
        functionBinding.setAdapter(this);
        return new TourViewHolder(functionBinding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        if (!TextUtils.isEmpty( (CharSequence) strings.get( position ).getImg() )) {
            GlideUtil.setUriMethod( mcontext,Constants.BASE + strings.get( position ).getImg(),tv.ivModuleStore );
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<StroeBean.GoodsListBean> {
        ItemStoreBinding functionBinding;
        ImageView ivModuleStore;

        public TourViewHolder(ItemStoreBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModuleStore = dataBinding.ivModuleStore;
        }

        @Override
        public void bindData(StroeBean.GoodsListBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

}
